//
//  BaseNaviPanel.m
//  AsyncImages
//
//  Created by Matthew on 11-12-28.
//  Copyright 2011年 develop4u.gr. All rights reserved.
//

#import "BaseNaviPanel.h"
#import "LetterButton.h"
#import "ColorUtil.h"

#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 480

#define PAGE_WIDTH 320
#define PAGE_HEIGHT 480

@implementation BaseNaviPanel

@synthesize tweenTableLink_Character;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        useTweenPanel = FALSE;
        infoPanel = [[BaseInfoPanel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        [self addSubview:infoPanel];
        
        table_tween = [[BaseInfoPanel alloc] initWithFrame:CGRectMake(PAGE_WIDTH,0 , PAGE_WIDTH, PAGE_HEIGHT)];
        [self addSubview:table_tween];
        
        detailPanel = [[BaseDetailPanel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        [self addSubview:detailPanel];
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

-(void) trigger:(NSString *) infoLink
 infoPanelTitle:(NSString *) inInfoPanelTitle
detailPanelLink:(NSString *) inDetailPanelLink
detailPanelTitle:(NSString *) inDetailPanelTitle
                suffix:(NSString *) inSuffix
                 type:(int) inType
{   
    detail_panel_shift_in = [[NSString alloc] initWithFormat:@"detail_panel_shift_in_%@",inSuffix];
    unit_trigger = [[NSString alloc] initWithFormat:@"unit_trigger_%@",inSuffix];
    detail_panel_shift_out = [[NSString alloc] initWithFormat:@"detail_panel_shift_out_%@",inSuffix];
    detail_view_exit = [[NSString alloc] initWithFormat:@"detail_view_exit_%@",inSuffix];
    page_refresh = [[NSString alloc] initWithFormat:@"page_refresh_%@",inSuffix];
    share_btn_trigger = [[NSString alloc] initWithFormat:@"share_btn_trigger_%@",inSuffix];
    
    tween_table_suffix = [[NSString alloc] initWithFormat:@"%@%@",inSuffix,@"tween_table"];
    unit_trigger_tween = [[NSString alloc] initWithFormat:@"unit_trigger_%@",tween_table_suffix];
    detail_view_exit_tween = [[NSString alloc] initWithFormat:@"detail_view_exit_%@",tween_table_suffix];

    // Initialization message listener
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSelector:) name:detail_panel_shift_in object:Nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSelector:) name:detail_panel_shift_out object:Nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSelector:) name:unit_trigger object:Nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSelector:) name:detail_view_exit object:Nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSelector:) name:page_refresh object:Nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSelector:) name:share_btn_trigger object:Nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSelector:) name:unit_trigger_tween object:Nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSelector:) name:detail_view_exit_tween object:Nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSelector:) name:@"Character_Panel_Trigger" object:Nil];
    
    infoPanel.topBarType = inType;
    [infoPanel trigger: infoLink 
              infoBarTitle: inInfoPanelTitle
                        suffix:inSuffix];
    firstTitle = [[NSString alloc] initWithString:inInfoPanelTitle];
    
    [detailPanel trigger: inDetailPanelLink
                infoBarTitle: inDetailPanelTitle
                          suffix:inSuffix];
    
    //the shilft panel
    [table_tween setUpLogoAndImg:FALSE featuredImg:FALSE];
    [table_tween setUpReturnBtn:TRUE];
    [table_tween setInnerSuffix:tween_table_suffix];
    
    suffix = [[NSString alloc] initWithString:inSuffix];
    
    self.backgroundColor = [ColorUtil stringHexToUIColor:@"0xf0f0f0"];
}

-(void) notificationSelector:(NSNotification *) notification
{
    NSString *curNotificationName = [notification name];
    
    if ([curNotificationName isEqualToString:detail_panel_shift_in] ||
        [curNotificationName isEqualToString:unit_trigger])
    {
        MWFeedItem *tempAtom = [notification object];
        
        if (useTweenPanel) 
        {
            NSString *theStr;
            if ([tempAtom.identifier isEqualToString:@""]) 
            {
                theStr = tempAtom.location;
            }
            else
            {
                theStr = tempAtom.identifier;
            }
            
            NSString *theId = (NSString *)CFURLCreateStringByAddingPercentEscapes(
                                                                                           NULL,
                                                                                           (CFStringRef)theStr,
                                                                                           NULL,
                                                                                           (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                                                                           kCFStringEncodingUTF8 );
            
            [table_tween trigger: [[NSString alloc] initWithFormat:@"%@%@",tweenTableLink,theId]
                    infoBarTitle: tempAtom.title
                          suffix:tween_table_suffix];
            
            [UIView transitionWithView:detailPanel  duration:0.4f 
                               options:UIViewAnimationCurveEaseOut  
                            animations:
             ^{
                 table_tween.transform = CGAffineTransformMakeTranslation(-PAGE_WIDTH,0);
             }
                            completion:NULL];
        }
        else
        {
            [detailPanel triggerByBuildId:tempAtom.identifier];
            
            [UIView transitionWithView:detailPanel  duration:0.4f 
                               options:UIViewAnimationCurveEaseOut  
                            animations:
             ^{
                 detailPanel.transform = CGAffineTransformMakeTranslation(-PAGE_WIDTH,0);
             }
                            completion:NULL];
        }

    }
    else if ([curNotificationName isEqualToString:detail_panel_shift_out] ||
             [curNotificationName isEqualToString:detail_view_exit])
    {
        [UIView transitionWithView:detailPanel duration:0.4f 
                           options:UIViewAnimationCurveEaseOut  
                        animations:
         ^{
             detailPanel.transform = CGAffineTransformMakeTranslation(0,0);
             [table_tween releaseSelectedNode];
             [infoPanel releaseSelectedNode];
         }
                        completion:NULL];  
    }
    else if([curNotificationName isEqualToString:unit_trigger_tween])
    {
        if (useTweenPanel) 
        {
            MWFeedItem *tempAtom = [notification object];
            
            [detailPanel setUpTitle:tempAtom.title];
            [detailPanel triggerByBuildId:tempAtom.identifier];
            
            [UIView transitionWithView:detailPanel  duration:0.4f 
                               options:UIViewAnimationCurveEaseOut  
                            animations:
             ^{
                 detailPanel.transform = CGAffineTransformMakeTranslation(-PAGE_WIDTH,0);
             }
                            completion:NULL];
        }
    }
    else if([curNotificationName isEqualToString:detail_view_exit_tween])
    {
        [UIView transitionWithView:detailPanel duration:0.4f 
                           options:UIViewAnimationCurveEaseOut  
                        animations:
         ^{
             table_tween.transform = CGAffineTransformMakeTranslation(0,0);
             [table_tween releaseSelectedNode];
             [infoPanel releaseSelectedNode];
         }
                        completion:NULL];  
    }
    else if ([curNotificationName isEqualToString:page_refresh])
    {
        [infoPanel refresh];
    }
    else if ([curNotificationName isEqualToString:share_btn_trigger])
    {
        [detailPanel showSharekit];
    }
    else if([curNotificationName isEqualToString:@"Character_Panel_Trigger" ])
    {
        LetterButton *tempLetterButton = [notification object];
        
        if ([tempLetterButton.host isEqualToString:suffix]) 
        {
            [table_tween trigger: [[NSString alloc] initWithFormat:@"%@%@",tweenTableLink_Character,tempLetterButton.letter]
                    infoBarTitle: firstTitle 
                          suffix:tween_table_suffix];
            
            [UIView transitionWithView:detailPanel  duration:0.4f 
                               options:UIViewAnimationCurveEaseOut  
                            animations:
             ^{
                 table_tween.transform = CGAffineTransformMakeTranslation(-PAGE_WIDTH,0);
             }
                            completion:NULL];
        }
    }
}

-(void) setAtomData:(NSString *) feedAndUnit
               atom:(NSString *) inAtomUnit
               feed:(NSString  *) feedUnit
{
    if (infoPanel) 
    {
        [infoPanel setAtomData:feedAndUnit
                          atom:inAtomUnit 
                          feed:feedUnit];
    }
}

-(void) configTable:(int) inTableWidth
        tableHeight:(int) inTableHeight
         tableWidth:(int) inTableRowHeight
     tableUnitStyle:(int) inTableUnitStyle
{
    if (infoPanel) 
    {
        [infoPanel configTable:inTableWidth
                   tableHeight:inTableHeight
                    tableWidth:inTableRowHeight
                tableUnitStyle:inTableUnitStyle];
    }
}

-(void) setUpTweenTable:(NSString *) inLink
{
    useTweenPanel = TRUE;
    tweenTableLink = [[NSString alloc] initWithString:inLink];
}

-(void) setUpTweenAtomData:(NSString *) feedAndUnit
                      atom:(NSString *) inAtomUnit
                      feed:(NSString  *) feedUnit
{
    if (table_tween) 
    {
        [table_tween setAtomData:feedAndUnit atom:inAtomUnit feed:feedUnit];
    }
}

-(void) setUpUseCharacterPanel
{
    if (infoPanel) 
    {
        [infoPanel setUpUseCharacterPanel];
    }
}

-(void) configTweenTable:(int) inTableWidth
        tableHeight:(int) inTableHeight
         tableWidth:(int) inTableRowHeight
     tableUnitStyle:(int) inTableUnitStyle
{
    if (table_tween) 
    {
        [table_tween configTable:inTableWidth
                   tableHeight:inTableHeight
                    tableWidth:inTableRowHeight
                tableUnitStyle:inTableUnitStyle];
    }
}

- (void)dealloc
{
    [super dealloc];
}

@end
